package cn.bjxy.shishaolong.ra_room.controller;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.ra_room.entity.Raroom;
import cn.bjxy.shishaolong.ra_room.entity.RaroomStudentRalation;
import cn.bjxy.shishaolong.ra_room.service.IRaroomService;
import cn.bjxy.shishaolong.ra_room.service.IRaroomStudentRalationService;
import cn.bjxy.shishaolong.system.controller.AbstractController;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author 时少龙
 * @Date 2019-04-20 12:17
 * @Version 1.0
 */
public class RaroomBaseController extends AbstractController {

    @Autowired
    protected IRaroomService raroomService;
    @Autowired
    private IRaroomStudentRalationService studentRalationService;

    public static final String ROOM_ID = "roomId";


    public static final int ROOM_STATUS_NOT_OPEN = 2100;
    public static final String ROOM_STATUS_NOT_OPEN_MSG = "答疑室处于非开通状态，无法操作";

    public static final int NOT_TEACHER_ROOM = 2101;
    public static final String NOT_TEACHER_ROOM_MSG = "没有找到该老师处于开通状态的答疑室";


    /**
     * 通过老师id取到答疑室id
     *
     * @return
     */
    protected Integer getRoomIdByTeacherId(Integer teacherId) {
        Raroom raroom = raroomService.getOne(new QueryWrapper<Raroom>().lambda()
                .eq(Raroom::getTeacherId, teacherId).isNull(Raroom::getDeletedAt));
        if (raroom == null) {
            throw new OperatorFailureException(NOT_TEACHER_ROOM, NOT_TEACHER_ROOM_MSG);
        }
        return raroom.getTid();
    }

    /**
     * 通过老师id和答疑室Id 判断是否创建答疑室的老师
     *
     * @return
     */
    protected boolean isRoomTeacher(Integer teacherId, Integer roomId) {
        Raroom raroom = raroomService.getOne(new QueryWrapper<Raroom>().lambda()
                .eq(Raroom::getTeacherId, teacherId).isNull(Raroom::getDeletedAt)
                .eq(Raroom::getTid, roomId));
        return (raroom != null);
    }

    /**
     * 判断答疑室状态
     *
     * @param roomStatus
     * @param teacherId
     * @return
     */
    protected boolean checkRaRoomStaus(Integer roomStatus, Integer teacherId) {
        List<Raroom> rarooms = raroomService.queryByCondition(
                new Raroom().setStatus(roomStatus).setTeacherId(teacherId));
        return (rarooms != null && rarooms.size() > 0);
    }

    /**
     * 判断老师是否已开通答疑室
     *
     * @param teacherId
     * @return
     */
    protected boolean isRoomOpen(Integer teacherId) {
        return checkRaRoomStaus(SysConst.RA_ROOM_STATUS_OK, teacherId);
    }

    /**
     * 判断答疑室是否开通
     *
     * @param roomId
     * @return
     */
    protected boolean roomIsOpen(Integer roomId) {
        Raroom room = raroomService.getById(roomId);
        return (room != null);
    }


    /**
     * 答疑室是否关闭
     *
     * @param teacherId
     * @return
     */
    protected boolean isRoomClose(Integer teacherId) {
        return checkRaRoomStaus(SysConst.RA_ROOM_STATUS_CLOSE, teacherId);
    }

    /**
     * 答疑室是否已删除
     *
     * @param teacherId
     * @return
     */
    protected boolean isRoomDeleted(Integer teacherId) {
        return checkRaRoomStaus(SysConst.RA_ROOM_STATUS_DELETED, teacherId);
    }

    //***********************学生相关*************************

    /**
     * 判断学生是否在某个答疑室中
     *
     * @return
     */
    protected boolean checkStudentInRoom(Integer studentId, Integer roomId) {
        RaroomStudentRalation studentRalation = studentRalationService.getOne(new QueryWrapper<RaroomStudentRalation>().lambda()
                .eq(RaroomStudentRalation::getStudentId, studentId)
                .eq(RaroomStudentRalation::getRoomId, roomId)
                .isNull(RaroomStudentRalation::getDeletedAt));
        return studentRalation != null;
    }


    // ======================== 返回值相关=========================
    protected ResultMsg retRoomStatusNotOpen() {
        return ResultMsg.create(ROOM_STATUS_NOT_OPEN, ROOM_STATUS_NOT_OPEN_MSG);
    }


}
